# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : editor.py
# Date      : 2019/7/24 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import os
import sys

import maya.cmds as mc
import maya.mel as mel
from configs.constants.global_constant import *

import scripts.commonCmds as con

class Editor(object):
    
    def __init__(self):
        pass

    def copyright(self,parent=None):
        labal = "Copyright (c) 2018 | Lin lingwei. All rights reserved."
        copyrightBtn = "linlingweiCopyRight"
        if parent:
            mc.button(copyrightBtn, l=labal, en=False, bgc=self.rgbConvert(54, 56, 58), ebg=True,parent=parent)
        else:
            mc.button(copyrightBtn, l=labal, en=False, bgc=self.rgbConvert(54, 56, 58), ebg=True)

        return copyrightBtn

    def rgbConvert(self,r, g, b):
        _r = r / 255.0
        _g = g / 255.0
        _b = b / 255.0
        return [_r, _g, _b]

    def isExist(self,uiName):
        if mc.window(uiName, ex=True):
            mc.deleteUI(uiName)
    
        if mc.layout(uiName, ex=True):
            mc.deleteUI(uiName)
        return
    
    def selfWindow(self,wh=[400, 600]):
    
        name = self.winName()
        title = ""
        for n in name:
            if n.isupper():
                n = " "+n
            title += n
        title = "- {} -".format(title)
        self.isExist(name)
        win = mc.window(name,t=title,wh=wh,ds=True)
        
        return win
    def selfWorkspaceControl(self):
        name = self.winName()
        self.isExist(name)
        mc.workspaceControl(name, retain=False, floating=True,dtm=["right",0]
                            # uiScript=lambda *args: self.selfWindow()
                            )
        return name
    def winName(self):
        return self.__class__.__name__
        
    def text(self,label="text",size=14,font = "STHeiti",color=(0,0,0)):
        
        _tt = """<p style="font-family:{font};font-size:{size}px;color:rgb{color}">{label}</p>"""\
            .format(font=font,label=label,size=size,color=color)
        # print _tt
        return _tt
    
    def progress_grp(self,tag,maxValue=10,parent=None):
        row = mc.rowLayout(nc=10,parent=parent)
        mc.checkBox(con.lname(tag,"cb"),l="",rs=True)
        
        mc.progressBar(con.lname(tag,"pb"),maxValue=maxValue, width=255)
        t_icon = os.path.join(gIconPath, "none.png",)
        mc.iconTextButton(con.lname(tag,"itb"), image=t_icon,w=25, h=25)
        mc.setParent('..')
        return row
    
    def progressWin(self, title, maxValue=100, status=None, func=None):
        # +-+------------------+
        # |-|  Doing Nothing   |
        # +--------------------+
        # | Sleeping: 40%      |
        # |                    |
        # | +----------------+ |
        # | |||||||          | |
        # | +----------------+ |
        # |                    |
        # | Hit ESC to Cancel  |
        # +--------------------+
    
        # Always use the progress dialog from a script, never directly
        # from the Script Editor.
    
        amount = 0
    
        mc.progressWindow(title=title,
                          progress=amount,
                          status=status,
                          isInterruptable=True)
        while True:
            # Check if the dialog has been cancelled
            if mc.progressWindow(query=True, isCancelled=True):
                break
        
            # Check if end condition has been reached
            if mc.progressWindow(query=True, progress=True) >= maxValue:
                break
        
            amount += 1
        
            mc.progressWindow(edit=True, progress=amount,
                              # status=('Sleeping: ' + `amount` + '%')
                              )
        
            a = func
    
        mc.progressWindow(endProgress=1)
        return

class Progress(Editor):
    def __init__(self):
        super(Progress,self).__init__()
        self._maxValue = 100
        self._status = "None"
    
    def win(self,title):
        # +-+------------------+
        # |-|  Doing Nothing   |
        # +--------------------+
        # | Sleeping: 40%      |
        # |                    |
        # | +----------------+ |
        # | |||||||          | |
        # | +----------------+ |
        # |                    |
        # | Hit ESC to Cancel  |
        # +--------------------+
    
        # Always use the progress dialog from a script, never directly
        # from the Script Editor.
    
        mc.progressWindow(title=title,
                          progress=0,
                          status=self._status,
                          isInterruptable=True)
        return
    
    
    def setStatus(self, strs):
        self._status = strs
        mc.progressWindow(edit=True,status=strs)
        return
    
    def setMaxValue(self,ints):
        mc.progressWindow(edit=True, maxValue=ints,)
        
        return
    
    def setValue(self,ints):
        mc.progressWindow(edit=True, progress=ints,)
        return
    def func(self,inputFunc):
        if mc.progressWindow(query=True, isCancelled=True):
            mc.progressWindow(endProgress=1)
        # Check if end condition has been reached
        if mc.progressWindow(query=True, progress=True) >= self._maxValue:
            mc.progressWindow(endProgress=1)
        inputFunc()
        return
    def endProgress(self):
        mc.progressWindow(endProgress=1)
        return
if __name__ == '__main__':
    pass